# encoding:utf-8
import jieba.posseg as pseg
import jieba.analyse
import re
from pathlib import Path
import os

file_dir="E:\周丹丽研究生\语言学python\语料"
for root,dirs,files in os.walk(file_dir):
	list_none=[]
	if files==list_none:
		continue

	p = Path(root)  # 初始化构造Path对象

	file_object_list = list(p.glob("./*_out.txt"))
	for file in file_object_list:
		str1=root+"/"+file.name.replace('_out.txt','')+'_chuli.txt'
	#print(str1)
		file_write=open(str1,'w',encoding='UTF-8')
		file_object = open(file, 'rU', encoding='UTF-8')


		dict_count_n={}
		dict_count_nr={}
		dict_count_nr1={}
		dict_count_nr2={}
		dict_count_nrj={}
		dict_count_nrf={}
		dict_count_ns={}
		dict_count_nsf={}
		dict_count_nt={}
		dict_count_nz={}
		dict_count_nl={}
		dict_count_ng={}
		dict_count_t={}
		dict_count_tg={}
		dict_count_s={}
		dict_count_f={}
		dict_count_vn={}
		dict_count_un={}
		for line in file_object:
			#我们自己搭建的纠错语料库
			patt="就"
			line=re.sub(patt,"就 ",line)
			patt1="我"
			line=re.sub(patt1,"我 ",line)
			patt2="会"
			line=re.sub(patt2," 会 ",line)
			seg_line=pseg.lcut(line)
			for i in seg_line:
				if i.word=="树":
					i.flag="n"
				#print(i.word+" "+i.flag)
			#语料库结束
			for w,f in seg_line:
				if f=='n':
					dict_count_n[w]=dict_count_n.get(w,0)+1
				elif f=='nr':
					dict_count_nr[w]=dict_count_nr.get(w,0)+1
				elif f=='nr1':
					dict_count_nr1[w]=dict_count_nr1.get(w,0)+1
				elif f=='nr2':
					dict_count_nr2[w]=dict_count_nr2.get(w,0)+1
				elif f=='nrj':
					dict_count_nrj[w]=dict_count_nrj.get(w,0)+1
				elif f=='nrf':
					dict_count_nrf[w]=dict_count_nrf.get(w,0)+1
				elif f=='ns':
					dict_count_ns[w]=dict_count_ns.get(w,0)+1
				elif f=='nsf':
					dict_count_nsf[w]=dict_count_nsf.get(w,0)+1
				elif f=='nt':
					dict_count_nt[w]=dict_count_nt.get(w,0)+1
				elif f=='nz':
					dict_count_nz[w]=dict_count_nz.get(w,0)+1
				elif f=='nl':
					dict_count_nl[w]=dict_count_nl.get(w,0)+1
				elif f=='ng':
					dict_count_ng[w]=dict_count_ng.get(w,0)+1
				elif f=='t':
					dict_count_t[w]=dict_count_t.get(w,0)+1
				elif f=='tg':
					dict_count_tg[w]=dict_count_tg.get(w,0)+1
				elif f=='s':
					dict_count_s[w]=dict_count_s.get(w,0)+1
				elif f=='f':
					dict_count_f[w]=dict_count_f.get(w,0)+1
				elif f=='vn':
					dict_count_vn[w]=dict_count_vn.get(w,0)+1
				elif f=='un':
					dict_count_un[w]=dict_count_un.get(w,0)+1
#print(dict_count)
		dict_count_n=sorted(dict_count_n.items(),key=lambda x:x[1],reverse=True)   #字典排序，第一个参数把字典元素化为成对的元组类型，第二个参数是选择 排序的是值而不是word，第三个参数是降序
		dict_count_nr=sorted(dict_count_nr.items(),key=lambda x:x[1],reverse=True)
		dict_count_nr1=sorted(dict_count_nr1.items(),key=lambda x:x[1],reverse=True)
		dict_count_nr2=sorted(dict_count_nr2.items(),key=lambda x:x[1],reverse=True)
		dict_count_nrj=sorted(dict_count_nrj.items(),key=lambda x:x[1],reverse=True)
		dict_count_nrf=sorted(dict_count_nrf.items(),key=lambda x:x[1],reverse=True)
		dict_count_ns=sorted(dict_count_ns.items(),key=lambda x:x[1],reverse=True)
		dict_count_nsf=sorted(dict_count_nsf.items(),key=lambda x:x[1],reverse=True)
		dict_count_nt=sorted(dict_count_nt.items(),key=lambda x:x[1],reverse=True)
		dict_count_nz=sorted(dict_count_nz.items(),key=lambda x:x[1],reverse=True)
		dict_count_nl=sorted(dict_count_nl.items(),key=lambda x:x[1],reverse=True)
		dict_count_ng=sorted(dict_count_ng.items(),key=lambda x:x[1],reverse=True)
		dict_count_t=sorted(dict_count_t.items(),key=lambda x:x[1],reverse=True)
		dict_count_tg=sorted(dict_count_tg.items(),key=lambda x:x[1],reverse=True)
		dict_count_s=sorted(dict_count_s.items(),key=lambda x:x[1],reverse=True)
		dict_count_f=sorted(dict_count_f.items(),key=lambda x:x[1],reverse=True)
		dict_count_vn=sorted(dict_count_vn.items(),key=lambda x:x[1],reverse=True)
		dict_count_un=sorted(dict_count_un.items(),key=lambda x:x[1],reverse=True)

#print(dict_count)
		file_write.writelines("n名词"+'\n')
		for i in dict_count_n:
			strline=''.join(str(i))
			file_write.writelines(strline+'\n')
		file_write.writelines("nr人名"+'\n')
		for i in dict_count_nr:
			strline=''.join(str(i))
			file_write.writelines(strline+'\n')
		file_write.writelines("nr1汉语姓氏"+'\n')
		for i in dict_count_nr1:
			strline=''.join(str(i))
			file_write.writelines(strline+'\n')
		file_write.writelines("nr2汉语名字"+'\n')
		for i in dict_count_nr2:
			strline=''.join(str(i))
			file_write.writelines(strline+'\n')
		file_write.writelines("nrj日语人名"+'\n')
		for i in dict_count_nrj:
			strline=''.join(str(i))
			file_write.writelines(strline+'\n')
		file_write.writelines("nrf音译人名"+'\n')
		for i in dict_count_nrf:
			strline=''.join(str(i))
			file_write.writelines(strline+'\n')
		file_write.writelines("ns地名"+'\n')
		for i in dict_count_ns:
			strline=''.join(str(i))
			file_write.writelines(strline+'\n')
		file_write.writelines("nsf音译地名"+'\n')
		for i in dict_count_nsf:
			strline=''.join(str(i))
			file_write.writelines(strline+'\n')
		file_write.writelines("nt机构团体名"+'\n')
		for i in dict_count_nt:
			strline=''.join(str(i))
			file_write.writelines(strline+'\n')
		file_write.writelines("nz其他专名"+'\n')
		for i in dict_count_nz:
			strline=''.join(str(i))
			file_write.writelines(strline+'\n')
		file_write.writelines("nl名词性惯用语"+'\n')
		for i in dict_count_nl:
			strline=''.join(str(i))
			file_write.writelines(strline+'\n')
		file_write.writelines("ng名词性语素"+'\n')
		for i in dict_count_ng:
			strline=''.join(str(i))
			file_write.writelines(strline+'\n')
		file_write.writelines("t时间词"+'\n')
		for i in dict_count_t:
			strline=''.join(str(i))
			file_write.writelines(strline+'\n')
		file_write.writelines("tg时间词性语素"+'\n')
		for i in dict_count_tg:
			strline=''.join(str(i))
			file_write.writelines(strline+'\n')
		file_write.writelines("s处所词"+'\n')
		for i in dict_count_s:
			strline=''.join(str(i))
			file_write.writelines(strline+'\n')
		file_write.writelines("f方位词"+'\n')
		for i in dict_count_f:
			strline=''.join(str(i))
			file_write.writelines(strline+'\n')
		file_write.writelines("vn名动词"+'\n')
		for i in dict_count_vn:
			strline=''.join(str(i))
			file_write.writelines(strline+'\n')
		file_write.writelines("un未知词"+'\n')
		for i in dict_count_un:
			strline=''.join(str(i))
			file_write.writelines(strline+'\n')